#pragma once
#include "uart/ProtocolSender.h"
#include "uart/UartContext.h"
#include "utils/BrightnessHelper.h"
#include "storage/StoragePreferences.h"
#include "hw/HardwareManager.h"
#include "utils/GpioHelper.h"
#include "utils/PWMHelper.h"

#define Beep_type 0 //0为无源蜂鸣器，1为有源

#define MENU_STATE 1 //0为气泡水和富氢水共有 ，1为只有气泡水，2为只有富氢水
BYTE MENU_num;

#define BEEP_IO    "GPIO_5"

static void testPwm() {
    /**
     * 参数1： 端口号
     * 参数2： 频率
     * 参数3： 占空比
     * 参数4： 极性
     *
     * 默认是使能状态
     */
    PWMHelper pwm(1, 3000, 50, 0);

    // 以下延时用于测试设置后的效果
    usleep(50000);

    // 设置频率
    pwm.setFreq(2700);
    // 设置占空比
    pwm.setDuty(100);
    usleep(50000);

    // 关闭
    pwm.setEnable(false);
    usleep(50000);

    // 打开
    pwm.setEnable(true);
    usleep(50000);
  }

// delay_ms 可以控制蜂鸣器响应时长，单位ms
static void _beep(uint32_t delay_ms) {
	if(mButton16Ptr->isSelected())
	{
#if Beep_type
	GpioHelper::output(BEEP_IO, 1);
	usleep(delay_ms * 1000);
	GpioHelper::output(BEEP_IO, 0);
#else
	testPwm();
#endif
	}
}

// 控件自定义蜂鸣器回调
static void _custom_beep_cb() {
	_beep(50);
}
/*
*此文件由GUI工具生成
*文件功能：用于处理用户的逻辑相应代码
*功能说明：
*========================onButtonClick_XXXX
当页面中的按键按下后系统会调用对应的函数，XXX代表GUI工具里面的[ID值]名称，
如Button1,当返回值为false的时候系统将不再处理这个按键，返回true的时候系统将会继续处理此按键。比如SYS_BACK.
*========================onSlideWindowItemClick_XXXX(int index) 
当页面中存在滑动窗口并且用户点击了滑动窗口的图标后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如slideWindow1;index 代表按下图标的偏移值
*========================onSeekBarChange_XXXX(int progress) 
当页面中存在滑动条并且用户改变了进度后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如SeekBar1;progress 代表当前的进度值
*========================ogetListItemCount_XXXX() 
当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表的总数目,XXX代表GUI工具里面的[ID值]名称，
如List1;返回值为当前列表的总条数
*========================oobtainListItemData_XXXX(ZKListView::ZKListItem *pListItem, int index)
 当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表当前条目下的内容信息,XXX代表GUI工具里面的[ID值]名称，
如List1;pListItem 是贴图中的单条目对象，index是列表总目的偏移量。具体见函数说明
*========================常用接口===============
*LOGD(...)  打印调试信息的接口
*mTextXXXPtr->setText("****") 在控件TextXXX上显示文字****
*mButton1Ptr->setSelected(true); 将控件mButton1设置为选中模式，图片会切换成选中图片，按钮文字会切换为选中后的颜色
*mSeekBarPtr->setProgress(12) 在控件mSeekBar上将进度调整到12
*mListView1Ptr->refreshListView() 让mListView1 重新刷新，当列表数据变化后调用
*mDashbroadView1Ptr->setTargetAngle(120) 在控件mDashbroadView1上指针显示角度调整到120度
*
* 在Eclipse编辑器中  使用 “alt + /”  快捷键可以打开智能提示
*/
#define JBDH_NUM 6
bool show_donghua_flag = true;
BYTE show_donghua_count = 0;
BYTE SDIndex = 0;
BYTE DH1Index = 0;
BYTE JBDH_num = 0;//主面渐变动画图标号
BYTE JBDHIndex = 0;
char path[50] = {0};

BYTE show_JBDHdonghua_flag = 0;

bool BeepState;
BYTE FDprogress;
BYTE BLprogress;
BYTE iceTemp;
bool chushuiSD_flag = false;
BYTE dataBuf[2];
BYTE dataBuf_A5[1] = {0xA5};
BYTE dataBuf_E5_B2[2] = {0xE5,0xB2};
BYTE dataBuf_A3[19] = {0xAA  ,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA3  ,0x00,0x00 };//A3指令
BYTE dataBufYG[19] = {0xAA  ,0x00,0x00,0x19,0x00,0x96,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00  ,0x00,0x00 };//指令

char text[20];
UINT16 TDS1_data;//原水
UINT16 TDS2_data;//纯水
BYTE PK_state;//排空标志位
BYTE LX1_lifetime;//滤芯1剩余寿命
BYTE LX2_lifetime;//滤芯2剩余寿命
BYTE LX3_lifetime;//滤芯3剩余寿命

UINT16 getCheckSumYG(const BYTE *pData) {
	UINT16 sum = 0;
	for (int i = 1; i < 17; ++i) {
		sum += pData[i];
	}
	return sum;
}

bool sendProtocolYG(BYTE *dataBuf) {
	// 校验码 Checksum
	dataBuf[17] = getCheckSumYG(dataBuf)>>8;
	dataBuf[18] = getCheckSumYG(dataBuf);

	return UARTCONTEXT->send(dataBuf, 19);
}

#if 1
#include "logic/VelocityTracker.h"
#define DEF_ROLL_SPEED			120
#define TURN_PAGE_MIN_SPEED		0.0001
#define TIMER_IDTOUCH 1
#define TIMER_TIMETOUCH 300
static int current_page = 0;//页面索引
namespace {
// 轮播类
class ScrollWindowWrapper {
public:
	ScrollWindowWrapper() :
		mScrollPageChangeListenerPtr(NULL),
		mCurrentPage(0),
		mTargetPage(-1),
		mStartDownTime(0),
		mRollSpeed(DEF_ROLL_SPEED),
		mCurRollSpeed(DEF_ROLL_SPEED),
		mIsRolling(false),
		mHasDown(false),
		isRegistered(false) {
		//current_page = 0;
		isDown = false;
		/*mRealPage(0),
		mstateVectorPtr(&lightArr),*/
	}

	class IScrollPageChangeListener {
	public:
		virtual ~IScrollPageChangeListener() { }
		virtual void onScrollPageChange(ScrollWindowWrapper *pScrollWindow, int page) = 0;
	};

	void resetPage(){
		mScrollPageChangeListenerPtr  = NULL;
		mCurrentPage = 0;
		mTargetPage = -1;
		mStartDownTime = 0;
		mRollSpeed = DEF_ROLL_SPEED;
		mCurRollSpeed = DEF_ROLL_SPEED;
		mIsRolling = false;
		mHasDown  = false;
		mControlsList.clear();
		//current_page = 0;
		isDown = false;
		/*mRealPage = 0,*/

	}

	void setScrollPageChangeListener(IScrollPageChangeListener *pListener) {
		mScrollPageChangeListenerPtr = pListener;
	}

	int getCurrentPage() const {
		// LOGD("%d",mCurrentPage);
		return mCurrentPage;
	}
	/*
	 * 从mControlsList最后一个窗口的位置后加上新窗口
	 */
	void addPage(ZKBase *pPage) {
		if (!mControlsList.empty()) { // 不是空的 需要移动
			const LayoutPosition &lastPos = mControlsList.back()->getPosition();
			LayoutPosition pos = pPage->getPosition();
			pos.mLeft = lastPos.mLeft + lastPos.mWidth;
			pPage->setPosition(pos);
		} else {
			mPosition = pPage->getPosition();
		}
		mControlsList.push_back(pPage);
	}
	/*
	 * 设置第current_page页显示在屏幕上,current_page为SUBLOGIC界面设置的静态变量
	 */
	void setPageShow(){
		mCurrentPage = current_page;
		movePage(0);
	}

	void setPageShow0(int page){
		mCurrentPage = page;
		movePage(0);
	}

	int getPageSize() const {
		return mControlsList.size();
	}
	// 判断页码是否到达边界 如果是 则给出相反的极值
	bool setPageSize()
	{
		static int j =0;
		j++;
		if(mCurrentPage < 0 ){
			mCurrentPage = getPageSize() -1;
			return true;
		}
		else if(mCurrentPage > getPageSize() -1){
			mCurrentPage = 0;
			return true;
		}
		return false;
	}
	//移动函数,一个页面的移动
	void movePage_01(int dx){
		LayoutPosition lp = mControlsList[mCurrentPage]->getPosition();
		lp.mLeft = dx;
		mControlsList[mCurrentPage]->setPosition(lp);
	}
	// 移动函数
	void movePage(int dx)
	{
		LayoutPosition lp = mControlsList[mCurrentPage]->getPosition();
		//LOGD("i=%d,lp.mLeft = %d,dx = %d   ,return\n",i,lp.mLeft,dx);
		if (lp.mLeft == dx) {
			return;
		}
		// 移动当前的一页
		lp.mLeft = dx;
		mControlsList[mCurrentPage]->setPosition(lp);
		// 移动 左边的一页窗口
		if (mCurrentPage > 0) {
			lp = mControlsList[mCurrentPage - 1]->getPosition();
			lp.mLeft = -mPosition.mWidth + dx;
			mControlsList[mCurrentPage - 1]->setPosition(lp);
		}
		// 移动右边的一页窗口
		if (mCurrentPage < getPageSize() - 1) {
			lp = mControlsList[mCurrentPage + 1]->getPosition();
			lp.mLeft = mPosition.mWidth + dx;
			mControlsList[mCurrentPage + 1]->setPosition(lp);
		}
		// 做成循环滑动的效果
		// 移动 左边的一页窗口
		/*if (mCurrentPage == 0) {
			int mCurrentPageTmp = getPageSize() - 1;
			lp = mControlsList[mCurrentPageTmp]->getPosition();
			lp.mLeft = -mPosition.mWidth + dx;
			mControlsList[mCurrentPageTmp]->setPosition(lp);
		}
		// 移动右边的一页窗口
		if ((mCurrentPage == getPageSize() - 1)) {
			int mCurrentPageTmp = 0;
			lp = mControlsList[mCurrentPageTmp]->getPosition();
			lp.mLeft = mPosition.mWidth + dx;
			mControlsList[mCurrentPageTmp]->setPosition(lp);
		}*/
		current_page = mCurrentPage;
	}

	void notifyScrollPageChange() {
		if (mScrollPageChangeListenerPtr) {
			mScrollPageChangeListenerPtr->onScrollPageChange(this, mCurrentPage);
		}
	}
// 判断可不可以滑动
	bool needToRoll() const {
		if (mTargetPage != -1) {
			return true;
		}

		if (!mControlsList.empty()) {
			return mControlsList[mCurrentPage]->getPosition().mLeft != 0;
		}

		return false;
	}
	// 通过滑动判断左移还是右移动
	bool onTouchEvent(const MotionEvent &ev)
	{
		//if(scroll_ok_flag)
		if(ev.mX>244 && ev.mX<1043 && ev.mY<410&&mWindow1Ptr->isWndShow())//限制滑动响应区域
		{
			if (mControlsList.empty() || mIsRolling)
			{
				return false;
			}

			switch (ev.mActionStatus) {
				case MotionEvent::E_ACTION_DOWN:
					if (mPosition.isHit(ev.mX, ev.mY)) {/*mPosition.isHit(ev.mX, ev.mY - 109*/
						mDownPoint.x = ev.mX;
						mDownPoint.y = ev.mY;
						mHasDown = true;
						mStartDownTime = clock();
					}
					break;

				case MotionEvent::E_ACTION_MOVE: {
					if (!mHasDown) {
						return false;
					}

					int dx = ev.mX - mDownPoint.x;
					if (((mCurrentPage == 0) && (dx > 0)) ||
						((mCurrentPage == getPageSize() - 1) && (dx < 0))) {//不能移动
						mDownPoint.x = ev.mX;
						dx = 0;
					}
					movePage(dx);

					// 用于计算滑动速度
					mVelocityTracker.addMovement(ev);

					break;
				}

				case MotionEvent::E_ACTION_UP:
				case MotionEvent::E_ACTION_CANCEL: {  // 抬手判断 向左滑动还是向右滑动
					if (!mHasDown) {
						return false;
					}
					isDown = false;
					//向下划屏
					if((ev.mY-mDownPoint.y)>150 && abs(ev.mX-mDownPoint.x)<150 && mDownPoint.y < 180){
						isDown = true;
					}
					mUpPoint.x = ev.mX;
					mUpPoint.y = ev.mY;
					mHasDown   = false;

					float speed = mVelocityTracker.getXVelocity();  // 获取x移动速度

					//static int j =0;
					if ((mUpPoint.x - mDownPoint.x > mPosition.mWidth / 3) ||
						(speed > TURN_PAGE_MIN_SPEED)) {
						mCurRollSpeed = mRollSpeed;
						JBDHIndex = JBDH_NUM;
						show_JBDHdonghua_flag = 2;
						if(JBDH_num>0)
							JBDH_num--;
						else
							JBDH_num = MENU_num;
						//LOGD(" -------right------- %d !!!\n",JBDH_num);
					} else if ((mUpPoint.x - mDownPoint.x < -mPosition.mWidth / 3) ||
						(speed < -TURN_PAGE_MIN_SPEED)) {
						mCurRollSpeed = -mRollSpeed;
						JBDHIndex = 0;
						if(show_JBDHdonghua_flag==1)
						{
							if(JBDH_num<MENU_num)
								JBDH_num++;
							else
								JBDH_num = 0;
						}
						//LOGD(" ++++++++left+++++++ %d !!!\n",JBDH_num);
						show_JBDHdonghua_flag = 1;

					} else {
						mCurRollSpeed = (mUpPoint.x - mDownPoint.x > 0) ? -mRollSpeed : mRollSpeed;
					}

					mVelocityTracker.reset();
					/*
					if (needToRoll())
					{   // 可以运行就启动计时器
						mIsRolling = true;
						if(!isRegistered){
							mActivityPtr->registerUserTimer(ID_SCROLLWINDOW_ROLL_TIMER, 1);
							isRegistered = true;
							//LOGD("!isRegistered\n");
						}
						else{
							mActivityPtr->unregisterUserTimer(ID_SCROLLWINDOW_ROLL_TIMER);
							mActivityPtr->registerUserTimer(ID_SCROLLWINDOW_ROLL_TIMER, 1);
							isRegistered = true;
							//LOGD("isRegistered\n");
						}
					}*/

					break;
				}
			}
		}

		return false;
	}

	bool onTimer(int id) {
		switch (id) {
		case ID_SCROLLWINDOW_ROLL_TIMER:
			if (needToRoll())
			{
				int curPage = mCurrentPage;  //获取页码
				LayoutPosition lp = mControlsList[curPage]->getPosition();
				int dx = lp.mLeft + mCurRollSpeed;
				if(isDown){
					dx = 0;
				}
				else if (mCurRollSpeed > 0) // 向右滚动
				{
					//LOGD("mCurRollSpeed %d",mCurRollSpeed);
					if (mTargetPage == -1)
					{
						if (lp.mLeft < 0)
						{	// 回滚
							if (dx > 0) {
								dx = 0;
							}
						}
						else
						{
							if (dx > mPosition.mWidth) {
								dx = 0;
								--mCurrentPage;
								setPageSize();
								notifyScrollPageChange();
							}
						}
					}
					else
					{
						if (dx > mPosition.mWidth) {
							dx -= mPosition.mWidth;
							//LOGD("__LINE__ = %d,i = %d\n",__LINE__,i);
							--mCurrentPage;
							setPageSize();
							notifyScrollPageChange();
							if (mCurrentPage == mTargetPage) {
								mTargetPage = -1;
								dx = 0;
							}
							curPage = mCurrentPage;
						}
					}
				}
				else // 向左滚动
				{
					if (mTargetPage == -1) {
						if (lp.mLeft > 0) {	// 回滚
							if (dx < 0) {
								dx = 0;
							}
						} else {
							if (dx < -mPosition.mWidth) {
								dx = 0;
								++mCurrentPage;
								setPageSize();
								notifyScrollPageChange();
							}
						}
					} else {
						if (dx < -mPosition.mWidth) {
							dx += mPosition.mWidth;
							++mCurrentPage;
							setPageSize();
							notifyScrollPageChange();
							if (mCurrentPage == mTargetPage) {
								mTargetPage = -1;
								dx = 0;
							}
							curPage = mCurrentPage;
						}
					}
				}
				movePage(dx);
				current_page = mCurrentPage;
				//mListViewPagePtr->refreshListView();
			}
			if (!needToRoll()) {
				mIsRolling = false;
				break;
			}
			return true;
		}
		return false;
	}
private:
	IScrollPageChangeListener *mScrollPageChangeListenerPtr;
	int mCurrentPage;
	int mTargetPage;
	VelocityTracker mVelocityTracker; // 速度追踪
	long mStartDownTime;
	uint32_t mRollSpeed;
	int mCurRollSpeed;
	bool mIsRolling;
	POINT mDownPoint;
	POINT mUpPoint;
	bool mHasDown;
	bool isRegistered;
	bool isDown;
	std::vector<ZKBase *> mControlsList;

	LayoutPosition mPosition;
};

};
static ScrollWindowWrapper sScrollWindowWrapper;
#endif

static void updateAnimationSD(){
	snprintf(path, sizeof(path), "SD/%d.png", SDIndex);
	//mTextViewSDPtr->setBackgroundPic(path);
	mButtonSDPtr->setButtonStatusPic(ZK_CONTROL_STATUS_NORMAL,path);
	SDIndex = (SDIndex + 1) % 53;
}

static void updateAnimation(){
	snprintf(path, sizeof(path), "DH%d/%d.png",JBDH_num, DH1Index);
	//snprintf(path, sizeof(path), "DH3/loading_%d.png", DH1Index);
	mTextviewD1Ptr->setBackgroundPic(path);
	DH1Index = (DH1Index + 1) % 13;
}

void deal_lock_TEMP()
{
	if(JBDH_num==1)
	{
		dataBufYG[3] = 0x2D;
		//mButtonLockPtr->setSelected(1);
		mButtonLockPtr->setVisible(true);
	}
	else if(JBDH_num==2)
	{
		dataBufYG[3] = 0x41;
		if(mButton15Ptr->isSelected())
			mButtonLockPtr->setSelected(1);
		mButtonLockPtr->setVisible(true);
	}
	else if(JBDH_num==3)
	{
		if(FDprogress<85)
			dataBufYG[3] = FDprogress;
		else
			dataBufYG[3] = 0x55;
		if(mButton15Ptr->isSelected())
			mButtonLockPtr->setSelected(1);
		mButtonLockPtr->setVisible(true);
	}
	else if(JBDH_num==4)
	{
		dataBufYG[3] = FDprogress;
		if(mButton15Ptr->isSelected())
			mButtonLockPtr->setSelected(1);
		mButtonLockPtr->setVisible(true);
	}
	else if(JBDH_num==5)
	{
		dataBufYG[3] = iceTemp;
		//mButtonLockPtr->setSelected(0);
		mButtonLockPtr->setVisible(false);

	}
	else if(JBDH_num==6||JBDH_num==7)
	{
		dataBufYG[3] = 3;
		//mButtonLockPtr->setSelected(0);
		mButtonLockPtr->setVisible(false);
	}
	else
	{
		dataBufYG[3] = 0x19;
		//mButtonLockPtr->setSelected(0);
		mButtonLockPtr->setVisible(false);
	}
}

static void JBDH_Animation_left(){
	JBDHIndex = (JBDHIndex + 1) % JBDH_NUM;

	if(MENU_STATE==0)
		snprintf(path, sizeof(path), "JBDH%d/%d.png",JBDH_num, JBDHIndex);
	else if(MENU_STATE==1)
		snprintf(path, sizeof(path), "JBDHq%d/%d.png",JBDH_num, JBDHIndex);
	else if(MENU_STATE==2)
		snprintf(path, sizeof(path), "JBDHf%d/%d.png",JBDH_num, JBDHIndex);
	mTextViewJBDHPtr->setBackgroundPic(path);

	if(JBDHIndex==JBDH_NUM-1)
	{
		if(JBDH_num<MENU_num)
			JBDH_num++;
		else
			JBDH_num = 0;

		show_JBDHdonghua_flag = 0;
		LOGD(" **********left JBDH_num*********** %d !!!\n",JBDH_num);
		deal_lock_TEMP();
	}
}


static void JBDH_Animation_right(){
	JBDHIndex = (JBDHIndex - 1) % JBDH_NUM;

	if(MENU_STATE==0)
		snprintf(path, sizeof(path), "JBDH%d/%d.png",JBDH_num, JBDHIndex);
	else if(MENU_STATE==1)
		snprintf(path, sizeof(path), "JBDHq%d/%d.png",JBDH_num, JBDHIndex);
	else if(MENU_STATE==2)
		snprintf(path, sizeof(path), "JBDHf%d/%d.png",JBDH_num, JBDHIndex);
	mTextViewJBDHPtr->setBackgroundPic(path);

	if(JBDHIndex==0)
	{
		show_JBDHdonghua_flag = 0;
		LOGD(" **********right JBDH_num*********** %d !!!\n",JBDH_num);
		deal_lock_TEMP();
	}
}

static bool isRegistered = false;
#define TIMER_HANDLE   2
#define TIMER_time   2000
/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	{0,  30}, //定时器id=0, 时间间隔6秒
	{1,  200},
	{3,  3000},
};

/**
 * 当界面构造时触发
 */
static void onUI_init(){
	mButtonLockPtr->setSelected(1);
	mButtonLockPtr->setVisible(false);

	if(MENU_STATE==0)
		MENU_num = 7;
	else
		MENU_num = 6;

	if(MENU_STATE==0)
		snprintf(path, sizeof(path), "JBDH0/0.png");
	else if(MENU_STATE==1)
		snprintf(path, sizeof(path), "JBDHq0/0.png");
	else if(MENU_STATE==2)
		snprintf(path, sizeof(path), "JBDHf0/0.png");
	mTextViewJBDHPtr->setBackgroundPic(path);


	HARDWAREMANAGER->setCustomBeep(_custom_beep_cb);
    //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");

	BeepState = StoragePreferences::getBool("BeepState", false);
	mButton16Ptr->setSelected(BeepState);

	FDprogress = StoragePreferences::getInt("FDprogress", 90);
	mSeekBar1Ptr->setProgress(FDprogress-80);
	mTextView6Ptr->setText(FDprogress);

	BLprogress = StoragePreferences::getInt("BLprogress", 80);
	mSeekBar2Ptr->setProgress(BLprogress-1);
	mTextView7Ptr->setText(BLprogress);
	BRIGHTNESSHELPER->setBrightness(BLprogress);

	iceTemp = StoragePreferences::getInt("iceTemp", 5);
	mSeekBar3Ptr->setProgress(iceTemp-5);
	mTextView8Ptr->setText(iceTemp);

	mCircleBar1Ptr->setProgress(LX1_lifetime);
	mCircleBar2Ptr->setProgress(LX2_lifetime);

	sScrollWindowWrapper.addPage(mWindow1Ptr);

	mButton150MLPtr->setSelected(1);
    dataBufYG[4] = 0x00;
    dataBufYG[5] = 0x96;

    UARTCONTEXT->send(dataBuf_E5_B2, 2);
	sprintf(text, "%d",TDS2_data);
	mTextViewTDSPtr->setText(text);
	sprintf(text, "%d%%",LX1_lifetime);
	mTextViewLXPtr->setText(text);

}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {

}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}


typedef struct{
	char iconNormal;
	char iconMiddle;
	char iconBig;
}S_ICONINFO;

const S_ICONINFO iconTab[]={
		'h','p','8',
		'a','i','1',
		'b','j','2',
		'c','k','3',
		'd','l','4',
		'e','m','5',
		'f','n','6',
		'g','o','7',
};


static void ShowPic(int num)
{
	mWindow1Ptr->hideWnd();
	mWindow2Ptr->hideWnd();
	mWindow21Ptr->hideWnd();
	mWindow22Ptr->hideWnd();
	mWindow23Ptr->hideWnd();
	mWindow231Ptr->hideWnd();
	mWindow232Ptr->hideWnd();
	mWindow24Ptr->hideWnd();
	mWindow25Ptr->hideWnd();
	mWindow26Ptr->hideWnd();

	switch (num)
	{
	case 1:
		mWindow1Ptr->showWnd();
		break;

	case 2:
		mWindow2Ptr->showWnd();
		break;

	case 21:
		mWindow21Ptr->showWnd();
		break;

	case 22:
		mWindow22Ptr->showWnd();
		break;

	case 23:
		mWindow23Ptr->showWnd();
		break;

	case 231:
		mWindow231Ptr->showWnd();
		break;

	case 232:
		mWindow232Ptr->showWnd();
		break;

	case 24:
		mWindow24Ptr->showWnd();
		break;

	case 25:
		mWindow25Ptr->showWnd();
		break;

	case 26:
		mWindow26Ptr->showWnd();
		break;

		break;
	default:
		break;
	}
}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {
		case ID_SCROLLWINDOW_ROLL_TIMER:
				return sScrollWindowWrapper.onTimer(id);

		case 0:
			if(show_JBDHdonghua_flag==1)
			{
				//mTextviewD1Ptr->setVisible(false);
				JBDH_Animation_left();
			}
			else if(show_JBDHdonghua_flag==2)
			{
				//mTextviewD1Ptr->setVisible(false);
				JBDH_Animation_right();
			}

			break;

		case 1:
			if(chushuiSD_flag)
				updateAnimationSD();
			/*if(show_JBDHdonghua_flag==0)
			{
				mTextviewD1Ptr->setVisible(true);
				updateAnimation();
			}*/
			break;

		case 3:
			if(chushuiSD_flag)
			{
				UARTCONTEXT->send(dataBuf_A5, 1);
			}


			UARTCONTEXT->send(dataBuf_E5_B2, 2);

			sprintf(text, "%d",TDS2_data);
			mTextViewTDSPtr->setText(text);
			sprintf(text, "%d%%",LX1_lifetime);
			mTextViewLXPtr->setText(text);

			if(mWindow23Ptr->isWndShow())
			{
				sprintf(text, "%d",TDS2_data);
				mTextView3Ptr->setText(text);

				mCircleBar1Ptr->setProgress(LX1_lifetime);
				mCircleBar2Ptr->setProgress(LX2_lifetime);
				LOGD("999999\n");
			}
			break;

		case TIMER_HANDLE://延时
	    	isRegistered = false;
		    return false;
			break;

		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onmainActivityTouchEvent(const MotionEvent &ev) {
	sScrollWindowWrapper.onTouchEvent(ev);//轮播对象赋值
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}
static int getListItemCount_ListView1(const ZKListView *pListView) {
    //LOGD("getListItemCount_ListView1 !\n");
    return 8;
}

static bool onButtonClick_Button1(ZKButton *pButton) {
    LOGD(" ButtonClick Button1 !!!\n");
	UARTCONTEXT->send(dataBuf_E5_B2, 2);
    ShowPic(2);
    return false;
}
static bool onButtonClick_Button2(ZKButton *pButton) {
    LOGD(" ButtonClick Button2 !!!\n");
    ShowPic(1);
    return false;
}
static bool onButtonClick_Button21(ZKButton *pButton) {
    LOGD(" ButtonClick Button21 !!!\n");
    ShowPic(21);
    return false;
}

static bool onButtonClick_Button22(ZKButton *pButton) {
    LOGD(" ButtonClick Button22 !!!\n");
    ShowPic(22);
    return false;
}

static bool onButtonClick_Button23(ZKButton *pButton) {
    LOGD(" ButtonClick Button23 !!!\n");
    ShowPic(23);
    return false;
}

static bool onButtonClick_Button24(ZKButton *pButton) {
    LOGD(" ButtonClick Button24 !!!\n");
    ShowPic(24);
    return false;
}

static bool onButtonClick_Button25(ZKButton *pButton) {
    LOGD(" ButtonClick Button25 !!!\n");
    ShowPic(25);
    return false;
}

static bool onButtonClick_Button26(ZKButton *pButton) {
    LOGD(" ButtonClick Button26 !!!\n");
    ShowPic(26);
    return false;
}
static bool onButtonClick_Button3(ZKButton *pButton) {
    LOGD(" ButtonClick Button3 !!!\n");
    ShowPic(2);
    return false;
}

static bool onButtonClick_Button4(ZKButton *pButton) {
    LOGD(" ButtonClick Button4 !!!\n");
    ShowPic(2);
    return false;
}

static bool onButtonClick_Button5(ZKButton *pButton) {
    LOGD(" ButtonClick Button5 !!!\n");
    ShowPic(2);
    return false;
}

static bool onButtonClick_Button6(ZKButton *pButton) {
    LOGD(" ButtonClick Button6 !!!\n");
    ShowPic(2);
    return false;
}

static bool onButtonClick_Button7(ZKButton *pButton) {
    LOGD(" ButtonClick Button7 !!!\n");
    ShowPic(2);
    return false;
}

static bool onButtonClick_Button8(ZKButton *pButton) {
    LOGD(" ButtonClick Button8 !!!\n");
    ShowPic(2);
    return false;
}
static bool onButtonClick_Button9(ZKButton *pButton) {
    LOGD(" ButtonClick Button9 !!!\n");
    ShowPic(231);
    return false;
}

static bool onButtonClick_Button10(ZKButton *pButton) {
    LOGD(" ButtonClick Button10 !!!\n");
    ShowPic(232);
    return false;
}

static bool onButtonClick_Button11(ZKButton *pButton) {
    LOGD(" ButtonClick Button11 !!!\n");
	dataBufYG[7] = 0x03;
	sendProtocolYG(dataBufYG);
    ShowPic(23);
    return false;
}

static bool onButtonClick_Button12(ZKButton *pButton) {
    LOGD(" ButtonClick Button12 !!!\n");
	dataBufYG[7] = 0x0C;
	sendProtocolYG(dataBufYG);
    ShowPic(23);
    return false;
}

void disable_ML(void)
{
	mButton150MLPtr->setSelected(0);
	mButton230MLPtr->setSelected(0);
	mButton300MLPtr->setSelected(0);
	mButton500MLPtr->setSelected(0);
	mButtonLXPtr->setSelected(0);
}

static bool onButtonClick_Button150ML(ZKButton *pButton) {
    LOGD(" ButtonClick Button150ML !!!\n");
    disable_ML();
    mButton150MLPtr->setSelected(1);
    dataBufYG[4] = 0x00;
    dataBufYG[5] = 0x96;
    return false;
}

static bool onButtonClick_Button230ML(ZKButton *pButton) {
    LOGD(" ButtonClick Button230ML !!!\n");
    disable_ML();
    mButton230MLPtr->setSelected(1);
    dataBufYG[4] = 0x00;
    dataBufYG[5] = 0xE6;
    return false;
}

static bool onButtonClick_Button300ML(ZKButton *pButton) {
    LOGD(" ButtonClick Button300ML !!!\n");
    disable_ML();
    mButton300MLPtr->setSelected(1);
    dataBufYG[4] = 0x01;
    dataBufYG[5] = 0x2C;
    return false;
}

static bool onButtonClick_Button500ML(ZKButton *pButton) {
    LOGD(" ButtonClick Button500ML !!!\n");
    disable_ML();
    mButton500MLPtr->setSelected(1);
    dataBufYG[4] = 0x01;
    dataBufYG[5] = 0xF4;
    return false;
}

static bool onButtonClick_Button13(ZKButton *pButton) {
    LOGD(" ButtonClick Button13 !!!\n");
    if(BLprogress>10)
    {
		BLprogress = BLprogress-10;
		BRIGHTNESSHELPER->setBrightness(BLprogress);
		mTextView7Ptr->setText(BLprogress);
		mSeekBar2Ptr->setProgress(BLprogress-1);
		StoragePreferences::putInt("BLprogress", BLprogress);
    }
    return false;
}
static bool onButtonClick_Button14(ZKButton *pButton) {
    LOGD(" ButtonClick Button14 !!!\n");
    if(BLprogress<100)
    {
		BLprogress = BLprogress+10;
		BRIGHTNESSHELPER->setBrightness(BLprogress);
		mTextView7Ptr->setText(BLprogress);
		mSeekBar2Ptr->setProgress(BLprogress-1);
		StoragePreferences::putInt("BLprogress", BLprogress);
    }
    return false;
}

static bool onButtonClick_ButtonLX(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonLX !!!\n");
    disable_ML();
    mButtonLXPtr->setSelected(1);
    dataBufYG[4] = 0x0B;
    dataBufYG[5] = 0xB8;
    return false;
}

static bool onButtonClick_ButtonLock(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonLock !!!\n");
    if(mButton15Ptr->isSelected())
    {
       if(dataBufYG[3]<=45)
         pButton->setSelected(!pButton->isSelected());
    }
    else
    	pButton->setSelected(!pButton->isSelected());
    return false;
}


static bool onButtonClick_ButtonSD(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonSD !!!\n");

    if(!mButtonLockPtr->isSelected()||!mButtonLockPtr->isVisible())
    {
		if(chushuiSD_flag)
		{
			chushuiSD_flag = false;
			dataBufYG[2] = 0x00;
			sendProtocolYG(dataBufYG);
		}
		else
		{
			chushuiSD_flag = true;
			dataBufYG[2] = 0x01;
			sendProtocolYG(dataBufYG);
		}
    }

    return false;
}
static bool onButtonClick_Button15(ZKButton *pButton) {
    LOGD(" ButtonClick Button15 !!!\n");
    pButton->setSelected(!pButton->isSelected());

    if(mButton15Ptr->isSelected())
    	mButtonLockPtr->setSelected(1);
    return false;
}
static void onProgressChanged_SeekBar1(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged SeekBar1 %d !!!\n", progress);
	FDprogress = progress+80;
	mTextView6Ptr->setText(FDprogress);
	StoragePreferences::putInt("FDprogress", FDprogress);
}
static void onProgressChanged_SeekBar2(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged SeekBar2 %d !!!\n", progress);
	BLprogress = progress+1;
	BRIGHTNESSHELPER->setBrightness(BLprogress);
	mTextView7Ptr->setText(BLprogress);
	StoragePreferences::putInt("BLprogress", BLprogress);
}

static bool onButtonClick_Button16(ZKButton *pButton) {
    LOGD(" ButtonClick Button16 !!!\n");
    pButton->setSelected(!pButton->isSelected());

    BeepState = pButton->isSelected();
	StoragePreferences::putBool("BeepState", BeepState);
    return false;
}

static void onProgressChanged_SeekBar3(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged SeekBar3 %d !!!\n", progress);
	iceTemp = progress+5;
	mTextView8Ptr->setText(iceTemp);
	StoragePreferences::putInt("iceTemp", iceTemp);
}
static bool onButtonClick_Button17(ZKButton *pButton) {
    LOGD(" ButtonClick Button17 !!!\n");
    ShowPic(1);
    return false;
}

static void onSlideItemClick_SlideWindow1(ZKSlideWindow *pSlideWindow, int index) {
    LOGD(" onSlideItemClick_ SlideWindow1 %d !!!\n", index);
    ShowPic(21+index);
}
static bool onButtonClick_ButtonRight(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonRight !!!\n");
	JBDHIndex = JBDH_NUM;
	show_JBDHdonghua_flag = 2;
	if(JBDH_num>0)
		JBDH_num--;
	else
		JBDH_num = MENU_num;

	//LOGD(" -------right------- %d !!!\n",JBDH_num);
    return false;
}

static bool onButtonClick_ButtonLeft(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonLeft !!!\n");
	JBDHIndex = 0;
	if(show_JBDHdonghua_flag==1)
	{
		if(JBDH_num<MENU_num)
			JBDH_num++;
		else
			JBDH_num = 0;
	}
	//LOGD(" ++++++++left+++++++ %d !!!\n",JBDH_num);
	show_JBDHdonghua_flag = 1;
    return false;
}
static bool onButtonClick_ButtonQCYS(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonQCYS !!!\n");
    sendProtocolYG(dataBuf_A3);
    return false;
}
